package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.BasePageQuery;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.UserService;
import com.tarena.lbs.pojo.basic.bo.UserBO;
import com.tarena.lbs.pojo.basic.bo.UserGroupBO;
import com.tarena.lbs.pojo.basic.param.UserGroupParam;
import com.tarena.lbs.pojo.basic.param.UserParam;
import com.tarena.lbs.pojo.basic.param.UserTagsParam;
import com.tarena.lbs.pojo.basic.query.UserGroupQuery;
import com.tarena.lbs.pojo.basic.vo.UserGroupVO;
import com.tarena.lbs.pojo.basic.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/basic/user/info/register")
    public Result<Void> saveUser(@RequestBody UserParam param) throws BusinessException {
        //调用业务层 新增注册
        UserBO userBO=new UserBO();
        BeanUtils.copyProperties(param,userBO);
        userService.saveUser(userBO);
        return Result.success();
    }
    //查询登录用户详情
    @GetMapping("/basic/user/detail")
    public Result<UserVO> userDetail()throws BusinessException{
        UserBO userBO=userService.userDetail();
        UserVO userVO=null;
        if (userBO!=null){
            userVO=new UserVO();
            BeanUtils.copyProperties(userBO,userVO);
        }
        return new Result<>(userVO);
    }

    //第一次登录 绑定用户的标签
    @GetMapping("/basic/user/tag")
    public Result<Void> bindUserTags(UserTagsParam param) throws BusinessException{
        //保证方法幂等
        //在同一个业务中 方法被多次执行 结果是相同的
        userService.bindUserTags(param);
        return Result.success();
    }

    @PostMapping("/admin/basic/userGroup/list")
    public Result<PageResult<UserGroupVO>> userGroupPage(@RequestBody UserGroupQuery query) throws BusinessException{
        BasePageList<UserGroupBO> boPage=userService.userGroupPage(query);
        //是否存在一个转化器
        return new Result<>(assemblePageVOS(boPage,query));
    }

    //新增用户人群
    @PostMapping("/admin/basic/userGroup/save")
    public Result<Void> saveUserGroup(@RequestBody UserGroupParam userGroupParam) throws BusinessException{
        UserGroupBO bo=new UserGroupBO();
        BeanUtils.copyProperties(userGroupParam,bo);
        userService.saveUserGroup(bo);
        return Result.success();
    }

    //转化器
    public PageResult<UserGroupVO> assemblePageVOS(BasePageList<UserGroupBO> bos, BasePageQuery query) {
        if (bos == null || bos.getTotal() == 0) {
            return null;
        }
        PageResult<UserGroupVO> result = new PageResult<>();
        List<UserGroupVO> vos = new ArrayList<>();
        for (UserGroupBO temp : bos.getList()) {
            UserGroupVO vo = new UserGroupVO();
            BeanUtils.copyProperties(temp,vo);
            vos.add(vo);
        }
        result.setTotal(bos.getTotal());
        result.setObjects(vos);
        result.setPageNo(query.getPageNo());
        result.setPageSize(query.getPageSize());
        return result;
    }
}
